package cn.edu.cqupt.cyExam.utils;



import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DateUtil
{
	/**
	 * 判断指定时间段period是否与时间段列表重叠
	 * @param period
	 * @param periodedList
	 * @return
	 * @throws ParseException 
	 */
	public static boolean periodIsOverlap(Map<String,String> period,List<Map<String,String>> periodedList) throws ParseException
	{
		boolean result = false;
		Date start = parseDate(period.get("term_startTime"));
		Date end = parseDate(period.get("term_endTime"));
		for(int i=0;i<periodedList.size();i++)
		{
			Date start_i = parseDate(periodedList.get(i).get("term_startTime"));
			Date end_i = parseDate(periodedList.get(i).get("term_endTime"));
			//开始时间在当前时间段
			if(start.after(start_i) && start.before(end_i))
			{
				return true;
				
			}
			//结束时间在当前时间段
			else if(end.after(start_i) && end.before(end_i))
			{
				return true;
			}
			else if(start.before(start_i) && end.after(end_i))
			{
				return true;
			}
			
		}
		return result;
	}
	/**
	 * 将日期字符串解析为一个日期对象
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date parseDate(String dateStr) throws ParseException
	{
		return new java.text.SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
	}
	/**
	 * 将指定的时间对象按"yyyy-MM-dd HH:mm:ss"返回其对应的字符串
	 * @param date
	 * @return
	 */
	public static String formatDateAndTime(Date date)
	{
		return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
	}
	public static String formatDateOnly(Date date)
	{
		return new java.text.SimpleDateFormat("yyyy-MM-dd").format(date);
	}
	public static String formatTimeOnly(Date date)
	{
		return new java.text.SimpleDateFormat("HH:mm:ss").format(date);
	}
	public static String formatTimeOnlyHasNotSecond(Date date)
	{
		return new java.text.SimpleDateFormat("HH:mm").format(date);
	}
	
	
//	public static void main(String[] args) throws ParseException
//	{
//		Map<String,String> period = new HashMap<String,String>();
//		period.put("term_startTime", "2013-09-10");
//		period.put("term_endTime", "2014-02-28");
//		
//		
//		List<Map<String,String>> periodedList = new ArrayList<Map<String,String>>();
//		Map<String,String> period1 = new HashMap<String,String>();
//		period1.put("term_startTime", "2012-09-03");
//		period1.put("term_endTime", "2013-01-04");
//		periodedList.add(period1);
//		
//		Map<String,String> period2 = new HashMap<String,String>();
//		period2.put("term_startTime", "2013-02-04");
//		period2.put("term_endTime", "2013-06-28");
//		periodedList.add(period2);
//		
//		System.out.println(periodIsOverlap(period,periodedList));
//		
//		
//		
//	}
}
